package com.renwen.sms.service.impl;

import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.renwen.sms.dao.AdminDao;
import com.renwen.sms.dao.StudentDao;
import com.renwen.sms.dao.TeacherDao;
import com.renwen.sms.dto.AdminDto;
import com.renwen.sms.dto.StudentDto;
import com.renwen.sms.dto.TeacherDto;
import com.renwen.sms.entity.Admin;
import com.renwen.sms.entity.Student;
import com.renwen.sms.entity.Teacher;
import com.renwen.sms.exception.MyRuntimeException;
import com.renwen.sms.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户登录业务逻辑接口实现类
 *
 * @author zuo, shunnian
 */
@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    private AdminDao adminDao;

    /**
     * 学生登录
     *
     * @param student 学生对象
     */
    @Override
    public StudentDto login(Student student, String code, HttpServletRequest request) {
        // 从上下文环境中获取验证码
        String vcode = (String) request.getServletContext().getAttribute("code");
        // 获取到的验证码与提交的验证码进行匹配
        if (!vcode.equalsIgnoreCase(code)) {
            throw new RuntimeException("输入验证码不对！");
        }
        //判断账号格式
        if (!ReUtil.isMatch("^\\w{10,20}$", student.getSNo())) {
            throw new RuntimeException("学号格式不对！");
        }
        //判断密码格式
        if (!ReUtil.isMatch("\\w{6,16}", student.getSPwd())) {
            throw new RuntimeException("密码是6~16位！");
        }
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s_no", student.getSNo());
        queryWrapper.select("nick_name", "s_pwd", "s_no", "s_name", "deng_ji", "id");

        Student student1 = studentDao.selectOne(queryWrapper);
        if (student1 == null) {
            throw new MyRuntimeException("输入的账号不存在！");
        } else if (!student1.getSPwd().equals(student.getSPwd())) {
            throw new MyRuntimeException("输入的密码不对！");
        }
        StudentDto studentDto = new StudentDto();
        //登陆时把学号放在session()里面
        request.getSession().setAttribute("sNo", student1.getSNo());
        //登出时候记得销毁session
        //request.getSession().invalidate();
        // 拷贝属性
        BeanUtils.copyProperties(student1, studentDto);
        return studentDto;
    }

    /**
     * 教师登录
     *
     * @param teacher 教师对象
     */
    @Override
    public TeacherDto login(Teacher teacher, String code, HttpServletRequest request) {
        // 从上下文环境中获取验证码
        String vcode = (String) request.getServletContext().getAttribute("code");
        // 获取到的验证码与提交的验证码进行匹配
        if (!vcode.equalsIgnoreCase(code)) {
            throw new RuntimeException("输入验证码不对！");
        }
        //判断账号格式
        if (!ReUtil.isMatch("^\\w{5,20}$", teacher.getTNo())) {
            throw new RuntimeException("教师号格式不对！");
        }
        //判断密码格式
        if (!ReUtil.isMatch("\\w{6,16}", teacher.getTPwd())) {
            throw new RuntimeException("密码是6~16位！");
        }
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t_no", teacher.getTNo());
        queryWrapper.select("t_no", "nick_name", "t_pwd", "t_name", "id");
        Teacher teacher1 = teacherDao.selectOne(queryWrapper);
        if (teacher1 == null) {
            throw new RuntimeException("输入的账号不存在！");
        } else if (!teacher1.getTPwd().equals(teacher.getTPwd())) {
            throw new RuntimeException("输入的密码不对！");
        }
        //登陆时把学号放在session()里面
        request.getSession().setAttribute("tNo", teacher1.getTNo());
        TeacherDto teacherDto = new TeacherDto();
        // 拷贝属性
        BeanUtils.copyProperties(teacher1, teacherDto);
        return teacherDto;
    }

    /**
     * 管理员登录
     *
     * @param admin 管理员对象
     */
    @Override
    public AdminDto login(Admin admin, String code, HttpServletRequest request) {
        // 从上下文环境中获取验证码
        String vcode = (String) request.getServletContext().getAttribute("code");
        // 获取到的验证码与提交的验证码进行匹配
        if (!vcode.equalsIgnoreCase(code)) {
            throw new RuntimeException("输入验证码不对！");
        }
        //判断账号格式
        if (!ReUtil.isMatch("^\\w{2,20}$", admin.getAdUsername())) {
            throw new RuntimeException("教师号格式不对！");
        }
        //判断密码格式
        if (!ReUtil.isMatch("\\w{6,16}", admin.getAdPwd())) {
            throw new RuntimeException("密码是6~16位！");
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ad_username", admin.getAdUsername());
        queryWrapper.select("ad_username", "nick_name", "ad_pwd", "real_name", "ad_no");
        Admin admin1 = adminDao.selectOne(queryWrapper);
        if (admin1 == null) {
            throw new RuntimeException("输入的账号不存在！");
        } else if (!admin1.getAdPwd().equals(admin.getAdPwd())) {
            throw new RuntimeException("输入的密码不对！");
        }
        AdminDto adminDto = new AdminDto();
        // 拷贝属性
        BeanUtils.copyProperties(admin1, adminDto);
        return adminDto;
    }

    /**
     * 管理员密码修改
     */
    @Override
    public void updatePassword1(String pass, String newPass, String adNo) {
        Integer adno = Integer.valueOf(adNo);
        //根据id查询用户
        Admin admin = adminDao.selectById(adno);
        //判断输入的旧密码是否正确
        if (admin == null) {
            throw new RuntimeException("用户不存在");
        }
        if (!pass.equals(admin.getAdPwd())) {
            throw new RuntimeException("输入的旧密码不正确");
        }
        //修改密码
        admin.setAdPwd(newPass);
        adminDao.updateById(admin);
    }

    /**
     * 学生密码修改
     */
    @Override
    public void updatePassword2(String pass, String newPass, String id) {
        Integer ID = Integer.valueOf(id);
        //根据id查询用户
        Student student = studentDao.selectById(ID);
        //判断输入的旧密码是否正确
        if (student == null) {
            throw new RuntimeException("用户不存在");
        }
        if (!pass.equals(student.getSPwd())) {
            throw new RuntimeException("输入的旧密码不正确");
        }
        //修改密码
        student.setSPwd(newPass);
        studentDao.updateById(student);
    }

    /**
     * 教师密码修改
     */
    @Override
    public void updatePassword3(String pass, String newPass, String id) {
        Integer ID = Integer.valueOf(id);
        //根据id查询用户
        Teacher teacher = teacherDao.selectById(ID);
        //判断输入的旧密码是否正确
        if (teacher == null) {
            throw new RuntimeException("用户不存在");
        }
        if (!pass.equals(teacher.getTPwd())) {
            throw new RuntimeException("输入的旧密码不正确");
        }
        //修改密码
        teacher.setTPwd(newPass);
        teacherDao.updateById(teacher);
    }
}
